<?php

class UserComment
{
    /** @var User */
    protected $user = null;
    /** @var User */
    protected $toUser = null;
    /** @var int */
    protected $releaseTime = null;
    /** @var int */
    protected $bigType = 0;
    /** @var int */
    protected $type = 0;
    /** @var CommodityComment|CommodityRequireComment */
    protected $comment = null;

    public function getUserDisplayName()
    {
        if ($this->getUser())
            return $this->getUser()->getDisplayName();
        return '[未知用户]';
    }

    public function getToUserDisplayName()
    {
        if ($this->getToUser())
            return $this->getToUser()->getDisplayName();
        return '[未知用户]';
    }

    public function getUserDisplayAvatarUrl()
    {
        if ($this->getUser())
            return $this->getUser()->getDisplayAvatarUrl();
        return base_url('public/images/default.gif');
    }

    public function getLinkDisplayTitle()
    {
        if ($this->getComment())
            return $this->getComment()->getLinkDisplayTitle();
        return '[未知标题]';
    }

    public function getFatherCommentDisplayContent()
    {
        if ($this->getComment()) {
            $father = $this->getComment()->getFatherComment();
            if ($father)
                return $father->getContent();
        }
        return '[未知内容]';
    }

    public function getCommentDisplayContent()
    {
        if ($this->getComment())
            return $this->getComment()->getContent();
        return '[未知内容]';
    }

    public function getCommodityIdOrCommodityRequireId()
    {
        if ($this->getComment()) {
            if ($this->getBigType() == 0)
                return $this->getComment()->getCommodityId();
            else if ($this->getBigType() == 1)
                return $this->getComment()->getCommodityRequireId();
        }
        return '0';
    }

    public function getUserId()
    {
        if ($this->getUser())
            return $this->getUser()->getId();
        return '0';
    }

    public function getCommentId()
    {
        if ($this->getComment())
            return $this->getComment()->getId();
        return '0';
    }

    public function getCommentDisplayTypeStr()
    {
        switch ($this->getType()) {
            case _UserCommentType_LiuYan:
                return '留言';
            case _UserCommentType_HuiFu:
                return '回复';
            case _UserCommentType_MiTan:
                return '密谈';
            case _UserCommentType_Zan:
                return '赞';
            default :
                return '未定义';
        }
    }

    public static function getUserCommentArrayByUserId($userId)
    {
        $comments = array();
        $commodityCommentArray = CommodityComment_Model::getCommodityCommentArrayByUserId($userId);
        foreach ($commodityCommentArray as $commodityComment) {
            /** @var CommodityComment $commodityComment */
            if ($commodityComment->isDeleted()) continue;
            $userComment = new UserComment();
            $userComment->setUser($commodityComment->getUser());
            $userComment->setToUser($commodityComment->getToUser());
            $userComment->setReleaseTime($commodityComment->getReleaseTime());
            $userComment->setComment($commodityComment);
            $userComment->setBigType(0);
            if (!$commodityComment->getFatherId())
                $userComment->setType(_UserCommentType_LiuYan);
            else
                $userComment->setType(_UserCommentType_HuiFu);
            if ($commodityComment->isPrivate())
                $userComment->setType(_UserCommentType_MiTan);
            $comments[] = $userComment;
        }
        $commodityRequireCommentArray = CommodityRequireComment_Model::getCommodityRequireCommentArrayByUserId($userId);
        foreach ($commodityRequireCommentArray as $commodityRequireComment) {
            /** @var CommodityRequireComment $commodityRequireComment */
            if ($commodityRequireComment->isDeleted()) continue;
            $userComment = new UserComment();
            $userComment->setUser($commodityRequireComment->getUser());
            $toUser = $commodityRequireComment->getToUser();
            if ($toUser)
                $userComment->setToUser($commodityRequireComment->getToUser());
            else
                $userComment->setToUser($commodityRequireComment->getMasterUser());
            $userComment->setReleaseTime($commodityRequireComment->getReleaseTime());
            $userComment->setBigType(1);
            if (!$commodityRequireComment->getFatherId())
                $userComment->setType(_UserCommentType_LiuYan);
            else
                $userComment->setType(_UserCommentType_HuiFu);
            if ($commodityRequireComment->isPrivate())
                $userComment->setType(_UserCommentType_MiTan);
            $userComment->setComment($commodityRequireComment);
            $comments[] = $userComment;
        }

        usort($comments, function ($a, $b) {
            /** @var UserComment $a */
            /** @var UserComment $b */
            if ($a->getReleaseTime() < $b->getReleaseTime()) return 1;
            else if ($a->getReleaseTime() == $b->getReleaseTime()) return 0;
            else return -1;
        });

        return $comments;
    }

    public static function getUserCommentDataByToUserIdOrMasterId($userId, $limit = false, $offset = false)
    {
        $comments = array();
        $commodityCommentArray = CommodityComment_Model::getCommodityCommentArrayByToUserIdOrMasterUserId($userId);
        foreach ($commodityCommentArray as $commodityComment) {
            /** @var CommodityComment $commodityComment */
            if ($commodityComment->isDeleted()) continue;
            $userComment = new UserComment();
            $userComment->setUser($commodityComment->getUser());
            $toUser = $commodityComment->getToUser();
            if ($toUser)
                $userComment->setToUser($commodityComment->getToUser());
            else
                $userComment->setToUser($commodityComment->getMasterUser());
            $userComment->setReleaseTime($commodityComment->getReleaseTime());
            $userComment->setComment($commodityComment);
            $userComment->setBigType(0);
            if (!$commodityComment->getFatherId())
                $userComment->setType(_UserCommentType_LiuYan);
            else
                $userComment->setType(_UserCommentType_HuiFu);
            if ($commodityComment->isPrivate())
                $userComment->setType(_UserCommentType_MiTan);
            $comments[] = $userComment;
        }
        $commodityRequireCommentArray = CommodityRequireComment_Model::getCommodityCommentArrayByToUserIdOrMasterUserId($userId);
        foreach ($commodityRequireCommentArray as $commodityRequireComment) {
            /** @var CommodityRequireComment $commodityRequireComment */
            if ($commodityRequireComment->isDeleted()) continue;
            $userComment = new UserComment();
            $userComment->setUser($commodityRequireComment->getUser());
            $toUser = $commodityRequireComment->getToUser();
            if ($toUser)
                $userComment->setToUser($commodityRequireComment->getToUser());
            else
                $userComment->setToUser($commodityRequireComment->getMasterUser());
            $userComment->setReleaseTime($commodityRequireComment->getReleaseTime());
            $userComment->setBigType(1);
            if (!$commodityRequireComment->getFatherId())
                $userComment->setType(_UserCommentType_LiuYan);
            else
                $userComment->setType(_UserCommentType_HuiFu);
            if ($commodityRequireComment->isPrivate())
                $userComment->setType(_UserCommentType_MiTan);
            $userComment->setComment($commodityRequireComment);
            $comments[] = $userComment;
        }

        usort($comments, function ($a, $b) {
            /** @var UserComment $a */
            /** @var UserComment $b */
            if ($a->getReleaseTime() < $b->getReleaseTime()) return 1;
            else if ($a->getReleaseTime() == $b->getReleaseTime()) return 0;
            else return -1;
        });

        $countComments = sizeof($comments);

        $comments = array_slice($comments, $offset, $limit);

        return array('comments' => $comments, 'countComments' => $countComments);
    }

    public static function countUserCommentArrayByToUserIdOrMasterId($userId)
    {
        $CommodityNum = CommodityComment_Model::countCommodityCommentArrayByToUserIdOrMasterUserIdWithoutDeleted($userId);
        $CommodityRequireNum = CommodityRequireComment_Model::countCommodityRequireCommentByToUserIdOrMasterUserIdWithoutDeleted($userId);
        return $CommodityNum + $CommodityRequireNum;
    }

    /**
     * @return CommodityComment|CommodityRequireComment
     */
    public function getComment()
    {
        return $this->comment;
    }

    /**
     * @param CommodityComment|CommodityRequireComment $comment
     */
    public function setComment($comment)
    {
        $this->comment = $comment;
    }

    /**
     * @return User
     */
    public function getToUser()
    {
        return $this->toUser;
    }

    /**
     * @param User $toUser
     */
    public function setToUser($toUser)
    {
        $this->toUser = $toUser;
    }

    /**
     * @return int
     */
    public function getReleaseTime()
    {
        return $this->releaseTime;
    }

    /**
     * @param int $releaseTime
     */
    public function setReleaseTime($releaseTime)
    {
        $this->releaseTime = $releaseTime;
    }

    /**
     * @return int
     */
    public function getBigType()
    {
        return $this->bigType;
    }

    /**
     * @param int $bigType
     */
    public function setBigType($bigType)
    {
        $this->bigType = $bigType;
    }

    /**
     * @return int
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param int $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * @return User
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param User $user
     */
    public function setUser($user)
    {
        $this->user = $user;
    }


}